const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime
}



//获取用户openid
module.exports.getOpenid = function getOpenid() {
  var app = getApp();
  //判断用户缓存数据
  if (app.globalData.openid == null) {
    wx.login({
      success: function (res) {
        console.log(res.code, '------0000000');
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wx3434b64fab872bfb&secret=1d37d5f6dbc4284f1b34da295162b2e9&js_code=' + res.code + '&grant_type=authorization_code',
          data: {},
          header: {
            'content-type': 'application/json'
          },
          success: function (resu) {
            console.log(resu, resu.data.openid, 'openid');
            
            wx.setStorageSync('openid', resu.data.openid);
            // app.globalData.openid = resu.data.openid; 
          }
        })
      },
      fail: function (res) {
        console.log('微信登录请求失败')
      },
      // complete: function (res) {}
    })
  }
}


//获取用户信息授权
module.exports.authorize = function authorize() {

  wx.openSetting({
    success: function (data) {
      if (data) {
        if (data.authSetting["scope.userInfo"] == true) {
          wx.authorize({
            scope: 'scope.userInfo',
            success() {
              console.log('authorize success(scope.userInfo)');
            },
            fail() {
              console.log('authorize fail(scope.userInfo)');
            }
          })
        }
      }
    }
  })

}


//收集、判断系统信息
module.exports.checkSystemInfo = function checkSystemInfo() {
  // 已使用的API
  //------------------
  // wx.getStorageSync v0
  // wx.setStorageSync v0
  //------------------
  // wx.login v0
  // wx.request v0
  // wx.requestPayment v0
  //------------------
  // wx.showModal v0
  // wx.showLoading v1.1.0
  // wx.hideLoading v1.1.0
  //------------------
  // wx.navigateTo v0
  // wx.navigateBack v0
  // wx.reLaunch v1.1.0
  var app = getApp();
  var info = wx.getSystemInfoSync();
  console.log(info);
  //检查微信兼容API
  if (wx.showLoading && wx.hideLoading && wx.reLaunch) {
  } else {
    app.alert('当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。');
  }
  app.globalData.systemInfo = info;
}




//检查页面层级
module.exports.checkPage = function checkPage() {
  var e = getCurrentPages();
  var l = e.length;
  console.log('当前页面层级' + l);
  console.log(e);
}

//获取当前时间
module.exports.getNowTime = function getNowTime(){

  var now = new Date(); var year = now.getFullYear(); 

  var month = now.getMonth() + 1; 

  var day = now.getDate(); 

  if (month < 10) { month = '0' + month; };

  if (day < 10) { day = '0' + day; }; 
  // 如果需要时分秒，就放开 // var h = now.getHours(); // var m = now.getMinutes(); // var s = now.getSeconds(); var formatDate = year + '-' + month + '-' + day; 
  
  // var formatDate = year + '-' + month + '-' + day;
  var formatDate = month + '-' + day;

  return formatDate;
}
//获取某一天的 昨天，明天，后天等的日期
module.exports.getDateStr = function getDateStr(addDayCount){
  var dd = new Date();

  dd.setDate(dd.getDate() + addDayCount);//获取AddDayCount天后的日期 

  var y = dd.getFullYear();

  var m = dd.getMonth() + 1;//获取当前月份的日期 

  var d = dd.getDate();

  var days = ['日', '一', '二', '三', '四', '五', '六'];
  var date = dd.getDay();//获取当前周几

  if (m < 10) {
    m = '0' + m;
  };
  if (d < 10) {
    d = '0' + d;
  };
  // return y + "-" + m + "-" + d;
  if (addDayCount==0){
    return m + "-" + d + '\n 今天';
  }
  if (addDayCount == 1){
    return m + "-" + d + '\n 明天';
  }
  return m + "-" + d + '\n 周' + days[date];

}
// https://www.jianshu.com/p/9645cd3461d3 小程序中的获取时间问题